package com.lb.uts.quartz;


import com.lb.uts.constants.UTSConstants;
import com.lb.uts.entity.ScheduleJob;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.context.ApplicationContext;

/**
 * 定时任务类基类
 *
 * @author liangb
 */
abstract class BaseQuartzJob implements Job {

    private ApplicationContext applicationContext;

    private ScheduleJob scheduleJob;

    /**
     * 任务执行的方法，由于反射生产的实例，该类的属性不能使用，改用 JobExecutionContext 传递参数
     */
    @Override
    public void execute(JobExecutionContext context)
            throws JobExecutionException {
        init(context);

        run(applicationContext, scheduleJob, context);
    }

    private void init(JobExecutionContext context) {
        applicationContext = (ApplicationContext) context.getMergedJobDataMap().get(UTSConstants.APP_CONTEXT);
        scheduleJob = (ScheduleJob) context.getMergedJobDataMap().get(UTSConstants.SCHEDULE_JOB);
    }


    protected abstract void run(ApplicationContext springContext, ScheduleJob job, JobExecutionContext jobContext);

}
